angular.module('start.user.ordercomplaint.ctrl', [])

  .controller('ordercomplaintCtrl', function($scope, $ionicHistory, $$UserOrderService, $rootScope, $stateParams, $location, $ionicLoading) {
    console.log("ordercomplaintCtrl");


    $scope.orderId = $stateParams.orderId;

    $scope.formData = {
      orderId: $scope.orderId,
      type: "",
      pic: "",
      content: ""
    };

    $scope.odcpt_selectsval = "";
    $scope.odcpt_selects = [
      { name: "社情骚扰", select: false },
      { name: "欺诈骗财", select: false },
      { name: "语言辱骂", select: false },
      { name: "人身攻击", select: false },
      { name: "广告传销", select: false },
      { name: "其他", select: false },
    ];

    if ($rootScope.orderDetail.complainComment == undefined) {} else {
      $scope.formData = {
        orderId: $scope.orderId,
        type: $rootScope.orderDetail.complainType,
        pic: $rootScope.orderDetail.complainPic,
        content: $rootScope.orderDetail.complainComment
      };

      var types = $rootScope.orderDetail.complainType.split(",");
      for (var i in types) {
        for (var j in $scope.odcpt_selects) {
          if (types[i] == $scope.odcpt_selects[j].name) {
            $scope.odcpt_selects[j].select = true;
          }
        }
      }

    }






    $scope.addselect = function(index) {
      $scope.odcpt_selectsval = "";
      $scope.odcpt_selects[index].select = !$scope.odcpt_selects[index].select;
      for (var i in $scope.odcpt_selects) {
        $scope.odcpt_selectsval +=
          $scope.odcpt_selects[i].select == true ? ($scope.odcpt_selects[i].name + ",") : "";
      }
      $scope.odcpt_selectsval = $scope.odcpt_selectsval.substr(0, $scope.odcpt_selectsval.length - 1);
      $scope.formData.type = $scope.odcpt_selectsval;
    };

    $scope.suborderComplt = function() {
      if (!checkText($scope.formData.type)) {
        $scope.alertPopup("请选择投诉类型");
        return false;
      }
      if (!checkText($scope.formData.content)) {
        $scope.alertPopup("请正确输入评价内容");
        return false;
      }
      $$UserOrderService.orderCompaint($scope.orderId, $scope.formData)
        .then(function(res) {
          $ionicLoading.show({
            template: "提交成功",
            noBackdrop: true,
            duration: 1500
          });
          $ionicHistory.goBack();
        }, function(err) {
          // $ionicHistory.goBack();
        });
    };

  });
